package com.dyh.quickdemozwei.subbreak;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.function.Consumer;

@Slf4j
public class App {
    public static void main(String[] args) {

        LinkedBlockingQueue<Integer> blockingQueue = new LinkedBlockingQueue<>(10);

        new Thread(()->{

            for (int i=0;i<10;i++){
                try {
                    blockingQueue.put(i);
                    try {
                        Thread.sleep(2L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            log.info("放任务结束");
        }).start();

        new Thread(()->{
            log.info("取任务");
            List<Integer> tasks  = new ArrayList<>(5);
            blockingQueue.drainTo(tasks,5);
            log.info("tasks={}", JSON.toJSONString(tasks));
            log.info("queue={}", JSON.toJSONString(blockingQueue));

        }).start();
    }
}
